import Vue from 'vue'
import Router from 'vue-router'



import login from '@/components/login'
import indexpage from '@/components/indexpage'
import indexcontent from '@/components/navMain/mainComponents/indexcontent'
import shoulikaidan from '@/components/navMain/mainComponents/shoulikaidan'
import piliangdaoru from '@/components/navMain/mainComponents/piliangdaoru'
import shoulidanguanli from '@/components/navMain/mainComponents/shoulidanguanli'
import yundanguanli from '@/components/navMain/mainComponents/yundanguanli'
// import checiguanli from '@/components/navMain/mainComponents/checiguanli'
import waichuandanguanli from '@/components/navMain/mainComponents/waichuandanguanli'
import huidan from '@/components/navMain/mainComponents/huidan'
import yichang from '@/components/navMain/mainComponents/yichang'
import shenhe from '@/components/navMain/mainComponents/shenhe'



Vue.use(Router)

const router = new Router({
        mode: 'history',
        base: process.env.BASE_URL,
        routes: [{
                path: '/',
                redirect: '/login'
            },
            {
                path: '/login',
                name: 'login',
                component: login
            },
            {
                path: '/indexpage',
                name: 'indexpage',
                component: indexpage,
                children: [{
                        path: '/index',
                        name: 'index',
                        component: indexcontent
                    },
                    {
                        path: '/shoulikaidan',
                        name: 'shoulikaidan',
                        component: shoulikaidan
                    }, {
                        path: '/piliangdaoru',
                        name: 'piliangdaoru',
                        component: piliangdaoru
                    }, {
                        path: '/shoulidanguanli',
                        name: 'shoulidanguanli',
                        component: shoulidanguanli
                    }, {
                        path: '/yundanguanli',
                        name: 'yundanguanli',
                        component: yundanguanli
                    },
                    // {
                    //   path: '/checiguanli',
                    //   name: 'checiguanli',
                    //   component: checiguanli
                    // },
                    {
                        path: '/waichuandanguanli',
                        name: 'waichuandanguanli',
                        component: waichuandanguanli
                    }, {
                        path: '/huidan',
                        name: 'huidan',
                        component: huidan
                    }, {
                        path: '/yichang',
                        name: 'yichang',
                        component: yichang
                    }, {
                        path: '/shenhe',
                        name: 'shenhe',
                        component: shenhe
                    }
                ]
            },

        ]


    })
    // 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // from 代表从哪个路径跳转而来
    // next 是一个函数 表示放行
    // next()放行  next('/login)表示强制跳转到登录页面
    // 如果访问的路径是login 那么直接放行
    if (to.path === '/login') return next()
        // 获得token的值
    const tokenStr = window.sessionStorage.getItem('token')
        // 如果tokenStr不存在那么直接跳转到登录页
    if (!tokenStr) return next('/login')
        // 否则tokenStr存在,那么直接跳转
    next()
})


export default router